package classes.Commands
{
	import UIUtils.uif_tb;
	import UIUtils.uif_util;
	
	import classes.c_util;
	
	import components.c_pcomponent;
	import components.editor.c_baseCollagesEditor;
	import components.editor.c_baseEditor;
	import components.editor.c_baseElement;
	import components.editor.c_bitmapElement;
	import components.editor.c_holeyElement;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class c_insertBitmapCommand extends c_commandFromEditor {
		private var _rect			: Rectangle;
		private var _source			: DisplayObject;
		private var _index			: int;
		private var _asholeObject	: Boolean;
		private var _params			: Object;
		
		public function c_insertBitmapCommand(a_editor: c_baseEditor, a_rect: Rectangle,
											  a_source: DisplayObject, a_asholeObject: Boolean=true, a_params: Object=null) {
			super(a_editor);
			_rect			= a_rect;
			_source			= a_source;
			_asholeObject 	= a_asholeObject;
			_params			= a_params;
		}
		
		override public function Execute():void {
			super.Execute();
			if (!_element) {
				var bitmapData: BitmapData = ((_source is Loader) && ((_source as Loader).content is Bitmap))?
					((_source as Loader).content as Bitmap).bitmapData:
					((_source is Bitmap)?((_source as Bitmap).bitmapData):null)
				if (bitmapData && bitmapData.transparent && _asholeObject) {
					var spots: Array = (_params && _params.spots)?_params.spots:uif_tb.searchSpots(bitmapData, 8);
					
					if (spots && spots.length && c_util.testSpotsOuter(spots, bitmapData.rect)) {
						_element = new c_holeyElement(editor.imageLayer, _rect, _source, spots);
						editor.dragRect.link = _element;
						return;
					}
				}
				_element = new c_bitmapElement(editor.imageLayer, _rect, _source);
				_index = _element.index;
			} else {
				_element.setParent(editor.imageLayer, _index);
			}
			editor.dragRect.link = _element;
		}
		
		override public function unExecute():void {
			_element.remove();
			super.unExecute();
		}
		
		override public function dispose():void {
			_element.dispose();
		}		
	}
}